//
//  SFExpandableTableViewController.m
//  TableViewFramework
//
//  Created by 支晓辉 on 16/3/24.
//  Copyright © 2016年 wishselfly. All rights reserved.
//

#import "SFExpandableTableViewController.h"

@interface SFExpandableTableViewController () 

@end

@implementation SFExpandableTableViewController
@dynamic tableView;

- (void)viewDidLoad {
    [super viewDidLoad];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.tableData.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (!self.tableData[section].expanded)return 0;
    return self.tableData[section].childData.count;
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    SFExpandableTableViewSectionGroup *srcGroup = self.tableData[sourceIndexPath.section],
                                      *destGroup = self.tableData[destinationIndexPath.section];
    id targetData = srcGroup.childData[sourceIndexPath.row];
    [srcGroup.childData removeObjectAtIndex:sourceIndexPath.row];
    [destGroup.childData insertObject:targetData atIndex:destinationIndexPath.row];
}

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *key = [NSString stringWithFormat:@"%ld,%ld", indexPath.section, indexPath.row];
    if (self.selectedIndexPath[key]) {
        [self.selectedIndexPath removeObjectForKey:key];
        self.tableData[indexPath.section].selectedCount--;
    } else {
        self.selectedIndexPath[key] = @(YES);
        self.tableData[indexPath.section].selectedCount++;
    }
    [tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationNone];
}

#pragma mark - Private methods

/**
 *  通过section,row这样的字符串转换成NSIndexPath
 *
 *  @param string 要转换的字符串
 *
 */
- (NSIndexPath*)getIndexPathFromString:(NSString*)string {
    NSArray *section_row = [string componentsSeparatedByString:@","];
    if (section_row.count != 2)return nil;
    NSIndexPath *path = [NSIndexPath indexPathForRow:[section_row[1] integerValue] inSection:[section_row[0] integerValue]];
    return path;
}

#pragma mark - Getters

- (NSMutableDictionary<NSString *,id> *)selectedIndexPath {
    if (!_selectedIndexPath) {
        _selectedIndexPath = [NSMutableDictionary dictionary];
    }
    return _selectedIndexPath;
}

- (NSArray *)selectedData {
    NSMutableArray *arr = [NSMutableArray array];
    NSMutableArray *sort_selectedIndexPathKeys = [self.selectedIndexPath.allKeys mutableCopy];
    [sort_selectedIndexPathKeys sortUsingComparator:^NSComparisonResult(NSString*  _Nonnull obj1, NSString*  _Nonnull obj2) {
        NSIndexPath *p1 = [self getIndexPathFromString:obj1],
                    *p2 = [self getIndexPathFromString:obj2];
        
        if (p1.section < p2.section) {
            return NSOrderedAscending;
        } else if (p1.section > p2.section) {
            return NSOrderedDescending;
        } else {
            if (p1.row < p2.row) {
                return NSOrderedAscending;
            } else if (p1.row > p2.row) {
                return NSOrderedDescending;
            }
        }
        
        return NSOrderedSame;
    }];
    [sort_selectedIndexPathKeys enumerateObjectsUsingBlock:^(NSString*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSIndexPath *path = [self getIndexPathFromString:obj];
        [arr addObject:self.tableData[path.section].childData[path.row]];
    }];
    
    return [arr copy];
}

#pragma mark - Setters

- (void)setTableData:(NSArray<SFExpandableTableViewSectionGroup *> *)tableData {
    _tableData = tableData;
    if ([NSThread isMainThread]) {
        [((UITableView*)self.view) reloadData];
    }
}

@end
